Ext.namespace("GridPartition");
GridPartition.mainGrid = function (p) {
	this.listPanel = p;
	this.detailPanel = new GridPartition.Detail(this);
	this.gridinfoPanel = new GridInfo.Detail(this);
	this.administrativeInfo = new GridAdministrative.Info();
	
	this.btnModify = new Ext.Toolbar.Button({
        iconCls: 'icon-modify',
        text: '修改区域',
        tooltip: {text: '修改所选区域信息'},
        handler: this.onModify,
        scope: this
    });
    
    this.btnMapView = new Ext.Toolbar.Button({
        text: '查看区域',
        iconCls: 'icon-map',
        tooltip: {text: '地图上查看区域'},
        handler: this.onMapView,
        scope: this
    });
    
    this.btnGisView = new Ext.Toolbar.Button({
        text: '网格导航',
        iconCls: 'icon-map',
        tooltip: {text: '网格导航'},
        handler: this.onGisView,
        scope: this
    });
    this.btnDrawGrid = new Ext.Toolbar.Button({
        text: '网格绘制',
        iconCls: 'icon-map',
        tooltip: {text: '网格导航'},
        handler: this.onDrawGrid,
        scope: this
    });
    
    this.btnAdministrative = new Ext.Toolbar.Button({
        text: '添加行政信息',
        iconCls: 'icon-add',
        tooltip: {text: '添加行政信息'},
        handler: this.addAdministrative,
        scope: this
    });
    
    this.store = new Ext.data.GroupingStore({
		proxy: new Ext.data.HttpProxy({
			url: '../gridareas/list.form?timesnap=' + Date.parse(new Date()),
			method: 'POST'
		}),
		reader: new Ext.data.JsonReader({
			root: 'grids',
			totalProperty: 'total',
			id: 'areaId',
			fields: ['areaId', 'name', 'type','gridManager','description','gisId','pid',{name:'gridManagerName', mapping: 'gridManagerUser.userName'}]
		}),
		params: {
    		start:0,
    		limit:5
    	}
	});
		
	GridPartition.mainGrid.superclass.constructor.call(this, {
		title: '区域列表',
		collapsible: true,
		animCollapse: false,
		region: 'north',
		columns: [
	        new Ext.grid.RowNumberer(),
			{id: 'areaId', dataIndex: 'areaId',hidden:true},
			{header: '区域名称', dataIndex: 'name',width:250,sortable:true},
			{header: '区域经理', dataIndex: 'gridManagerName',width:100,sortable:true},
			{header: '区域简介', dataIndex: 'description',width:350,sortable:true}
	    ],
		height:220,
		loadMask: true,
		//view: new Ext.grid.GroupingView({
        //   forceFit:false,
        //    groupTextTpl: '{text} ({[values.rs.length]} {[values.rs.length > 1 ? "项" : "项"]})'
        // }),
        tbar:[this.btnModify,this.btnGisView],
		bbar: new Ext.PagingToolbar({
			pageSize:5,
			store: this.store,
			//hidden:true,
			displayInfo: true,
			displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
			emptyMsg: "没有记录"
		}),
		listeners: {
			scope: this,
			rowclick : function (gird,rowIndex,e){
			var areaId = this.store.getAt(rowIndex).data.areaId;
			this.listPanel.regionId = areaId;
			this.listPanel.store.load({
					params:{
						regionId: areaId,
						start:0,
						limit:10
					}
				});			
			}
		}
    });
    this.on('dblclick', this.onModify, this);
};

Ext.extend(GridPartition.mainGrid, Ext.grid.GridPanel, {
	onModify: function(btn, event) {
		if (this.detailPanel == null) {
			this.detailPanel = new GridPartition.Detail(this);
		}
		var record = this.getSelectionModel().getSelected();
		if(record!=null){ 
			this.detailPanel.show();
			this.detailPanel.setValues(record.data);
		}else{		
			Ext.MessageBox.alert('提示','请先选中一个区域!');
		}
	},
	addAdministrative: function(btn, event) {
		if (this.administrativeInfo == null) {
			this.administrativeInfo = new GridAdministrative.Info(this);
		}
		//var record = this.getSelectionModel().getSelected();
		/*if(record!=null){ 
			this.detailPanel.show();
			this.detailPanel.setValues(record.data);
		}else{		
			Ext.MessageBox.alert('提示','请先选中一个区域!');
		}*/
		this.administrativeInfo.show();
	},
	onMapView: function(btn, event) {
		var record = this.getSelectionModel().getSelected();
		if(record!=null){ 			
			var gisId = record.data.gisId;
			var type = record.data.type;
			if (gisId==null||gisId=='') {
				return;
			}
			var url = OMS.GIS_IP+'/gis_for_management_simple.jsp?did=' + gisId +  '&type=' +type;
			Utils.openWindow(url,'网格地图');
		}else{		
			Ext.MessageBox.alert('提示','请先选中一个区域!');
		}
	},
	onGisView: function(btn, event) {		
		var connSyncSession = new Ext.data.Connection({
			url : '../security/getSessionUser.form?timesnap=' + Date.parse(new Date()),
			method : 'POST'
		});
		Utils.request(connSyncSession, {
			success : function(resp, action) {		
				var name = (resp.user)?resp.user.userId:'';
				var url = OMS.GIS_IP+'/privilege/session!sync?targetUrl=/gis.html&name='+name;
				window.open(url,'网格导航',"left=0,top=0,height=680,width=1024,toolbar=no,menubar=no,location=no,status=no,scrollbars=auto,resizable=yes");
			}
		});
		//var url = OMS.GIS_IP+'/privilege/session!sync?target=gis.html&name=panj';
		//window.open(url,'网格导航',"left=0,top=0,height=680,width=1024,toolbar=no,menubar=no,location=no,status=no,scrollbars=auto,resizable=yes");
			
	},
	onDrawGrid: function(btn, event) {
		var url = OMS.GIS_IP+'/gis_for_management.jsp';
		window.open(url,'网格绘制',"left=0,top=0,height=680,width=1024,toolbar=no,menubar=no,location=no,status=no,scrollbars=auto,resizable=yes");
	},
	update: function() {
		this.store.reload();
	}
});

GridPartition.subGrid = function() {
	this.gridinfoPanel = new GridInfo.Detail(this);
	this.regionId = null;
	
	this.btnModify = new Ext.Toolbar.Button({
        iconCls: 'icon-modify',
        text: '修改网格',
        tooltip: {text: '修改所选网格信息'},
        handler: this.onModify,
        scope: this
    });
    
    this.btnMapView = new Ext.Toolbar.Button({
        text: '查看网格',
        iconCls: 'icon-map',
        tooltip: {text: '地图上查看网格'},
        handler: this.onMapView,
        scope: this
    });
    
    this.btnDeleteGrid = new Ext.Toolbar.Button({
        text: '删除网格',
        iconCls: 'icon-delete',
        tooltip: {text: '删除网格'},
        handler: this.onDeleteGrid,
        scope: this
    });
    
    this.btnAddGird = new Ext.Toolbar.Button({
        text: '添加网格',
        iconCls: 'icon-add',
        tooltip: {text: '添加网格信息'},
        handler: this.addGridInfo,
        scope: this
    });
    
	this.store = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({
			url: '../gridareas/listGrid.form?timesnap=' + Date.parse(new Date()),
			method: 'POST'
		}),
		reader: new Ext.data.JsonReader({
			root: 'grids',
			totalProperty: 'total',
			id: 'gridId',
			fields: ['gridId', 'gridName','gridType','gridManager','areaId','gisId','gridStatus','disableDate','enableDate','hillId',{name:'gridManagerName', mapping: 'gridManagerUser.userName'}]
		}),
	 	params: {
			regionId:this.regionId,
    		start:0,
    		limit:10
    	},
    	listeners:{
	    	beforeload:{
		    	fn : function(thiz,options){
			          Ext.apply(thiz.baseParams,
			          { 
			        	regionId:this.regionId
			          });    
		    },
		    scope:this
		    }
	    }
	});

	GridPartition.subGrid.superclass.constructor.call(this, {
		title: '网格列表',
		collapsible: true,
		columns:[
	        new Ext.grid.RowNumberer(),
			{id: 'gridId',dataIndex: 'gridId',hidden:true},
			{header: '网格名称', dataIndex: 'gridName',width:250,sortable:true},
			{header: '网格经理', dataIndex: 'gridManagerName',width:100,sortable:true},
			{header: '网格状态', dataIndex: 'gridStatus',width:350,sortable:true}
	    ],
		region: 'center',
		Height:300,
		loadMask: {msg: '加载中，请稍候...'},
		//viewConfig: { forceFit: true },
		tbar:[this.btnAddGird,this.btnModify,this.btnDeleteGrid],
		bbar: new Ext.PagingToolbar({
			pageSize:10,
			store: this.store,
			displayInfo: true,
			displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
			emptyMsg: "没有记录"
		})
	});
	this.on('dblclick', this.onModify, this);
};

Ext.extend(GridPartition.subGrid, Ext.grid.GridPanel, {	
	onModify: function(btn, event) {
		if (this.gridinfoPanel == null) {
			this.gridinfoPanel = new GridInfo.Detail(this);
		}
		var record = this.getSelectionModel().getSelected();
		if(record!=null){ 
			this.gridinfoPanel.show();
			this.gridinfoPanel.initData(record.data.gridId);
		}else{		
			Ext.MessageBox.alert('提示','请先选中一个网格!');
		}
	},
	addGridInfo:function(btn,event){
		this.gridinfoPanel = new GridInfo.Detail(this);
		this.gridinfoPanel.show();
	},
	onDeleteGrid: function(btn, event) {		
		var connDelete = new Ext.data.Connection({
			url: '../gridareas/deleteGrid.form?timesnap=' + Date.parse(new Date()),
			method: 'POST'
		});
		
		var record = this.getSelectionModel().getSelected();
		if(record!=null){
			Ext.MessageBox.confirm("提示","确认删除  '"+record.data.gridName+"' ？",function(btn){
            if(btn=="yes"){
                Utils.request(connDelete, {
					params: {
						'gridId': record.data.gridId
					},
					scope: this,
					success: function(resp, action) {
						this.scope.store.reload();
					},
					after: function() {}
				});
            }   
            else{
               }
			},this);
			
		}else{		
			Ext.MessageBox.alert('提示','请先选中一条记录!');
		}
	},
	onMapView: function(btn, event) {
		var record = this.getSelectionModel().getSelected();
		if(record!=null){ 			
			var gisId = record.data.gisId;
			var type = record.data.type;
			if (gisId==null||gisId=='') {
				return;
			}
			var url = OMS.GIS_IP+'/gis_for_management_simple.jsp?did=' + gisId +  '&type=' +type;
			Utils.openWindow(url,'网格地图');
		}else{		
			Ext.MessageBox.alert('提示','请先选中一个网格!');
		}
		
	},
	update: function() {
		this.store.reload();
	}
});

var subGrid = null;
GridPartition.main=function(){
	subGrid = new GridPartition.subGrid();
	var mainGrid = new GridPartition.mainGrid(subGrid);
	mainGrid.store.load();
    var mainview = new Ext.Viewport({
        layout:'border',
        items:[mainGrid, subGrid]
    });
};

Ext.onReady(function() {
    Ext.QuickTips.init();
    Ext.form.Field.prototype.msgTarget = 'side';
    Ext.BLANK_IMAGE_URL = '../resources/images/default/s.gif';
    GridPartition.main();
});